package net.libra.ui
{
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;

	/**
	 * @author elvisjiang
	 */	
	public class NumStepper extends EventDispatcher
	{
		private var _text:TextField;
		
		public var max:int;
		public function NumStepper(textField:TextField,upBtn:EasyButton,downBtn:EasyButton,max:int = int.MAX_VALUE)
		{
			_text = textField;
			_text.type = TextFieldType.INPUT;
			_text.restrict = "0-9";
			_text.selectable = true;
			_text.addEventListener(Event.CHANGE,handler);
			this.max = max;
			_text.maxChars = this.max.toString().length;
			upBtn.addEventListener(MouseEvent.CLICK,upBtn_click);
			downBtn.addEventListener(MouseEvent.CLICK,downBtn_click);
		}
		
		protected function handler(event:Event):void
		{
			if(value >= max)
			{
				value = max;
			}
		}
		
		protected function downBtn_click(event:MouseEvent):void
		{
			if(value<=0){
				return;
				value = 0;
			}
			value --;
		}
		
		protected function upBtn_click(event:MouseEvent):void
		{
			if(value < max)
				value ++ ;
		}
		
		public function get value():int
		{
			if(_text.text == "")
			{
				return 0;
			}else{
				return int(_text.text);
			}
		}
		
		public function set value(value:int):void
		{
			_text.text = value + "";
		}
		
		public function destroy():void
		{
//			upBtn.removeEventListener(MouseEvent.CLICK,upBtn_click);
//			upBtn.destroy();
//			upBtn = null;
//			downBtn.removeEventListener(MouseEvent.CLICK,downBtn_click);
//			downBtn.destroy();
//			downBtn = null;
			_text.removeEventListener(Event.CHANGE,handler);
			_text = null;
		}
	}
}